package com.sdtv.basemodule.config.life;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * @ProjectName: MediunFusion
 * @Package: com.sdtv.basemodule.config.life
 * @ClassName: ActivityLifecycleCallBack
 * @Description: --生命周期管理
 * @Author: 小郭同学
 * @CreateDate: 2019-06-28 09:50
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class ActivityLifecycleCallBack implements Application.ActivityLifecycleCallbacks {

    private static final String TAG = "ActivityLifecycleCallBa";

    private static ActivityLifecycleCallBack instance;

    public static ActivityLifecycleCallBack getInstance() {
        synchronized (ActivityLifecycleCallBack.class) {
            if (null == instance) {
                instance = new ActivityLifecycleCallBack();
            }
        }
        return instance;
    }

    public void init(Application application) {
        application.registerActivityLifecycleCallbacks(this);
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
        // 把 activity加入栈中
        AppManager.getInstance().addActivity(activity);
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {

    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {

    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {

    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

    }

    /**
     * 销毁activity
     *
     * @param activity
     */
    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {
        // 降activity从栈中移除
        AppManager.getInstance().removeActivity(activity);
    }
}
